//
//  AnswerInputView.m
//  DoingPaper_LS
//
//  Created by zhuxuhong on 2016/11/5.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "AnswerInputView.h"
#import <Masonry/Masonry.h>

@interface AnswerInputView()<UITextViewDelegate>

@property(nonatomic,strong)UISegmentedControl *segment;
@property(nonatomic,strong)UIImageView *answerImageView;
@property(nonatomic,strong)UITextView *inputTextView;

@property(nonatomic,copy)NSString *inputedText;
@property(nonatomic,copy)UIImage *inputedImage;

@end


@implementation AnswerInputView
{
    NSInteger section;
}
-(instancetype)initWithFrame:(CGRect)frame sectionIndex:(NSInteger)index{
    self = [self initWithFrame:frame];
    section = index;
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
//    self.backgroundColor = [UIColor darkGrayColor];
    
    _answerImageView = [UIImageView new];
    _answerImageView.hidden = true;
    
    [self addSubview:self.segment];
    [self addSubview:self.inputTextView];
    [self addSubview:_answerImageView];

    CGFloat off20 = 20;
    [_segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(off20));
        make.top.equalTo(@(off20));
        make.width.equalTo(@(140));
        make.height.equalTo(@(30));
    }];
    
    // 固定等比例高度
    [_inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_segment);
        make.top.mas_equalTo(_segment.mas_bottom).offset(off20);
        make.right.equalTo(@(-off20));
        make.height.equalTo(@(100));
    }];
    [_answerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_inputTextView);
    }];
}

#pragma mark - getters
-(UISegmentedControl *)segment{
    if (!_segment) {
        _segment = [[UISegmentedControl alloc] initWithItems:@[
            @"输入",@"草稿",@"拍照"
        ]];
        _segment.selectedSegmentIndex = 0;
        [_segment addTarget:self action:@selector(segmentDidChangeValue:) forControlEvents:UIControlEventValueChanged];
    }
    return _segment;
}

-(UITextView *)inputTextView{
    if (!_inputTextView) {
        _inputTextView = [UITextView new];
        _inputTextView.delegate = self;
        _inputTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _inputTextView.layer.borderWidth = 1;
        _inputTextView.layer.cornerRadius = 6;
        _inputTextView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _inputTextView.font = [UIFont systemFontOfSize:16];
        _inputTextView.keyboardType = UIKeyboardTypeDefault;
        _inputTextView.returnKeyType = UIReturnKeyNext;
        
        // placeholder
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(14, 10, 140, 20)];
        label.text = @"输入答案";
        label.textColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.8];
        label.font = [UIFont systemFontOfSize:16];
        label.tag = 1000;
        [_inputTextView addSubview:label];
    }
    return _inputTextView;
}

-(NSString *)inputedText{
    NSString *text = [_inputTextView.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
    return text;
}


#pragma mark - actions
-(void)segmentDidChangeValue: (UISegmentedControl*)segment{
    switch (segment.selectedSegmentIndex) {
        case 0:
            
            break;
        case 1:
            
            break;
        case 2:
            
            break;
        default:
            break;
    }
}

#pragma mark - UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView{
    UILabel *placeLabel = [textView viewWithTag:1000];
    if ([textView.text isEqualToString:@""]) {
        placeLabel.text = @"输入答案";
    }
    else{
        placeLabel.text = @"";
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    if ([_delegate respondsToSelector:@selector(answerInputView:sectionIndex:didEndInputWithInputText:)]) {
        [self.delegate answerInputView:self sectionIndex:section
              didEndInputWithInputText:self.inputedText];
    }
}

// 回车
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {// return key
        if ([_delegate respondsToSelector:@selector(answerInputViewDidEndInputWithReturnKeyClick)]) {
            [self.delegate answerInputViewDidEndInputWithReturnKeyClick];
        }
        [_inputTextView endEditing:true];
        return false;
    }
    return true;
}


@end
